package report

import (
	"context"

	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/utils"
	reportRepo "git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/repository/report"
	pb "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/service/report"
	"git.mycaigou.com/ycg/common/v2/copier"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
)

type Report struct {
	reportService report.ReportServiceIface
}

func NewReport(reportService report.ReportServiceIface) pb.ReportServiceServer {
	return &Report{
		reportService: reportService,
	}
}

func (s *Report) GenerateUrl(ctx context.Context, request *pb.GenerateUrlRequest) (*pb.GenerateUrlResponse, error) {
	stark.Logger.Infof(ctx, "grpc GenerateUrl request:%+v", request)
	var response = &pb.GenerateUrlResponse{}
	response.IsSuccess = false

	reportShare := &reportRepo.ReportShare{}
	err := copier.Copy(reportShare, request.Report)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GenerateUrl request:%+v, err:%s", request, err.Error())
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, "参数错误")
	}
	_, err = s.reportService.GenerateUrl(ctx, reportShare)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GenerateUrl request:%+v, err:%s", request, err.Error())
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}

	response.IsSuccess = true
	response.Code = reportShare.Uid
	stark.Logger.Infof(ctx, "grpc GenerateUrl request:%+v, response:%+v", request, response)
	return response, nil
}

func (s *Report) ReceiveUrl(ctx context.Context, request *pb.ReceiveUrlRequest) (*pb.ReceiveUrlResponse, error) {
	stark.Logger.Infof(ctx, "grpc ReceiveUrl request:%+v", request)
	var response = &pb.ReceiveUrlResponse{}
	response.IsSuccess = false

	isSuccess, err := s.reportService.ReceiveUrl(ctx, request.GetRecipientUid(), request.GetCode())
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ReceiveUrl request:%+v, err:%s", request, err.Error())
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}

	response.IsSuccess = isSuccess
	stark.Logger.Infof(ctx, "grpc ReceiveUrl request:%+v, response:%+v", request, response)
	return response, nil
}

func (s *Report) ReportFromSharingList(ctx context.Context, request *pb.ReportFromSharingListRequest) (*pb.ReportFromSharingListResponse, error) {
	stark.Logger.Infof(ctx, "grpc ReportFromSharingList request:%+v", request)
	var response = &pb.ReportFromSharingListResponse{}

	condition := make(map[string]interface{})
	if request.RecipientUid != "" {
		condition["recipient_uid"] = request.RecipientUid
	}
	if request.ProvinceCode != "" && request.ProvinceCode != "0" {
		condition["province_code"] = request.ProvinceCode
	}
	if request.CityCode != "" {
		condition["city_code"] = request.CityCode
	}
	if request.ReportStatusCode != 0 {
		condition["report_status_code"] = request.ReportStatusCode
	}
	total, reportShareList, err := s.reportService.ReportFromSharingList(ctx, condition, &utils.PageInfo{
		PageSize: int(request.PageSize),
		Page:     int(request.Page),
	}, "receive_on desc")
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ReportFromSharingList request:%+v, err:%s", request, err.Error())
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}

	var responseReport []*pb.ReportFromSharing
	for _, val := range reportShareList {
		item := &pb.ReportFromSharing{
			TenantCode:         val.TenantCode,
			SharerUserId:       val.SharerUserId,
			SharerUserTenantId: val.SharerUserTenantId,
			SharerUserName:     val.SharerUserName,
			RecipientUid:       val.RecipientUid,
			ClueSource:         val.ClueSource,
			ClueSourceId:       val.ClueSourceId,
			ClueSourceTitle:    val.ClueSourceTitle,
			ReportStatus:       val.ReportStatus,
			ProvinceCode:       val.ProvinceCode,
			Province:           val.Province,
			CityCode:           val.CityCode,
			City:               val.City,
			CompanyName:        val.CompanyName,
			SubmitterName:      val.SubmitterName,
			CreatedOn:          utils.TimeToString(val.CreatedOn),  //val.CreatedOn.Format("2006-01-02 15:04:05"),
			ModifiedOn:         utils.TimeToString(val.ModifiedOn), //val.ModifiedOn.Format("2006-01-02 15:04:05"),
			SharerUserCode:     val.SharerUserCode,
			Uid:                val.Uid,
			ReceiveOn:          utils.TimeToString(val.ReceiveOn), //val.ReceiveOn.Format("2006-01-02 15:04:05"),
			TenantReportId:     val.TenantReportId,
			ReportStatusCode:   val.ReportStatusCode,
		}
		responseReport = append(responseReport, item)
	}

	response.Reports = responseReport
	response.Total = total
	stark.Logger.Infof(ctx, "grpc ReportFromSharingList request:%+v, response:%+v", request, response)
	return response, nil
}

func (s *Report) ReportInfo(ctx context.Context, request *pb.ReportInfoRequest) (*pb.ReportInfoResponse, error) {
	stark.Logger.Infof(ctx, "grpc ReportInfo request:%+v", request)

	var response = &pb.ReportInfoResponse{}
	var pbrs = &pb.ReportFromSharing{}

	condition := make(map[string]interface{})
	if request.GetCode() != "" {
		condition["uid"] = request.GetCode()
	}
	if request.GetReportId() != "" {
		condition["tenant_report_id"] = request.GetReportId()
	}
	rs, err := s.reportService.ReportInfo(ctx, condition)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ReportInfo request:%+v, err:%+v", request, err.Error())
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}

	err = copier.Copy(pbrs, rs)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ReportInfo request:%+v, copier copy err:%+v", request, err.Error())
		return response, err
	}
	response.Report = pbrs
	stark.Logger.Infof(ctx, "grpc ReportInfo request:%+v, response:%+v", request, response)
	return response, nil
}

func (s *Report) UpdateReportShare(ctx context.Context, request *pb.UpdateReportShareRequest) (*pb.UpdateReportShareResponse, error) {
	stark.Logger.Infof(ctx, "grpc UpdateReportShare request:%+v", request)
	var response = &pb.UpdateReportShareResponse{}

	isSuccess, err := s.reportService.UpdateReportShareStatus(ctx, request.RecipientUid, request.Code, request.Status)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc UpdateReportShare request:%+v，err:%s", request, err.Error())
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}

	response.IsSuccess = isSuccess
	stark.Logger.Infof(ctx, "grpc UpdateReportShare request:%+v, response:%+v", request, response)
	return response, nil
}
